package com.museum.guide.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.museum.guide.common.result.Result;
import com.museum.guide.dto.LoginDTO;
import com.museum.guide.dto.LoginResponseDTO;
import com.museum.guide.dto.RegisterDTO;
import com.museum.guide.entity.User;
import com.museum.guide.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public Result<LoginResponseDTO> login(@RequestBody @Valid LoginDTO loginDTO) {
        LoginResponseDTO loginResponse = userService.login(loginDTO);
        return Result.success(loginResponse);
    }

    /**
     * 用户注册
     */
    @PostMapping("/register")
    public Result<Void> register(@RequestBody @Valid RegisterDTO registerDTO) {
        userService.register(registerDTO);
        return Result.success();
    }

    /**
     * 分页查询用户
     */
    @GetMapping("/page")
    public Result<Page<User>> page(Page<User> page, String username) {
        return Result.success(userService.page(page, username));
    }

    /**
     * 新增用户
     */
    @PostMapping("add")
    public Result<Void> save(@RequestBody @Valid User user) {
        userService.saveUser(user);
        return Result.success();
    }

    /**
     * 更新用户
     */
    @PostMapping("update")
    public Result<Void> update(@RequestBody @Valid User user) {
        userService.updateUser(user);
        return Result.success();
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/removeById")
    public Result<Void> remove(Long id) {
        userService.removeUser(id);
        return Result.success();
    }
}